const express = require('express');
const router = express.Router();
const GoodModel = require('../models/goods');
const CartModel = require('../models/shopCart');

router.post('/shopCart/:id', function(req, res) {
	var id = req.params.id;
	//	console.log(id);
	var userId = req.cookies.userId;

	GoodModel.findById(id).exec(function(err, data) {
		//		console.log(data);
		if(err) {
			return res.json({
				error: 0,
				msg: '错误'
			})
		}
		if(data) {
			req.body.checked = 0;
			req.body.showShop = true;
			req.body.daiPay = true;
			req.body.waitGo = true;
			req.body.waitshou = true;
			req.body.waitPing = true;
			req.body.buyId = userId;
			req.body.author = data.author;
			req.body.price = data.price;
			req.body.title = data.title;
			req.body.shopImg = data.shopImg;

			var cartModel = new CartModel(req.body);
			cartModel.save(function(err) {
				if(err) {
					return res.json({
						error: 1,
						msg: '错误'
					})
				}
				res.json({
					error: 0,
					msg: '添加成功'
				});
			})
		}
	})
})
router.post('/gouwuche', function(req, res) {
	var id = req.cookies.userId;

	CartModel.find({
		showShop: true,
		buyId: id
	}).exec(function(err, data) {

		if(err) {
			return res.json({
				error: 1,
				msg: '错误'
			});
		}
		for(var i = 0; i < data.length; i++) {
			data[i].shopImg = '/images/' + data[i].shopImg
		}
		res.json({
			error: 0,
			msg: '成功',
			data
		})

	})
})
//购物车删除操作
router.post('/remove', function(req, res) {
	var id = req.body.id;

	CartModel.findByIdAndRemove(id).exec(function(err) {
		if(err) {
			return res.json({
				error: 1,
				msg: '错误'
			});
		}
		res.json({
			error: 0,
			msg: '删除成功'
		})

	})
})
//数量加减改变
router.post("/cartEdit", function(req, res, next) {
	var id = req.body.id,
		checked = req.body.checked,
		buyCount = req.body.buyCount;
	//	console.log(id)
	CartModel.update({
		"_id": req.body.id
	}, {
		"buyCount": req.body.buyCount
	}).exec(function(err, doc) {

		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}
		res.json({
			status: '0',
			msg: "成功",
			result: ''
		});

	})
	//      
});
//待付款
router.post('/payshopMessage', function(req, res) {
	var id = req.cookies.userId;
	CartModel.find({
		showShop: false,
		daiPay: true,
		buyId: id
	}).exec(function(err, data) {

		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}
		for(var i = 0; i < data.length; i++) {
			data[i].shopImg = '/images/' + data[i].shopImg
		}
		res.json({
			status: '0',
			msg: "成功",
			result: {
				data
			}
		});

	})
})
//daipingjia

//
router.post('/jiesuan', function(req, res) {
	var id = req.cookies.userId;
	CartModel.find({
		buyId: id
	}).exec(function(err, data) {
		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}
		data.forEach(function(item) {
			item.showShop = false;
			item.save();
		})
		res.json({
			error: 0,
			msg: '121',
			result: {
				data
			}
		})
	})
})
//待发货
router.post('/payshopMessages', function(req, res) {
	var id = req.cookies.userId;
	CartModel.find({
		showShop: false,
		daiPay: false,
		waitGo: true,
		buyId: id
	}).exec(function(err, data) {

		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}
		for(var i = 0; i < data.length; i++) {
			data[i].shopImg = '/images/' + data[i].shopImg
		}
		res.json({
			status: '0',
			msg: "成功",
			result: {
				data
			}
		});
	})
})
//待收货
router.post('/waitshou', function(req, res) {
	var id = req.cookies.userId;
	CartModel.find({
		showShop: false,
		daiPay: false,
		waitGo: false,
		waitshou: true,
		buyId: id
	}).exec(function(err, data) {

		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}
		for(var i = 0; i < data.length; i++) {
			data[i].shopImg = '/images/' + data[i].shopImg
		}
		res.json({
			status: '0',
			msg: "成功",
			result: {
				data
			}
		});
	})
})
//待评价

//全部订单
router.post('/allshop', function(req, res) {
	var id = req.cookies.userId;
	CartModel.find({
		buyId: id
	}).exec(function(err, data) {

		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}
//		for(var i = 0; i < data.length; i++) {
//			data[i].shopImg = '/images/' + data[i].shopImg
//		}
		res.json({
			status: '0',
			msg: "成功",
			result: {
				data
			}
		});
	})
})
//shanchu全部订单中的
router.post('/delshop', function(req, res) {
	CartModel.remove().exec(function(err) {
		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}
		res.json({
				error: 0,
				msg: '成功',
				
			})


	})

})

//单个付款使其出现在待发货中
router.post('/zhifu', function(req, res) {
	var id = req.body._id;
	CartModel.findById(id).exec(function(err, data) {

		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}

		data.daiPay = false;
		data.save();
		res.json({
			error: 0,
			msg: 'success',
			result: {
				data
			}
		})
	})
})
//点击评价
router.post('/waitping', function(req, res) {
	var id = req.cookies.userId
	CartModel.find({
		"buyId":id,
		showShop: false,
		daiPay: false,
		waitGo: false,
		waitshou: false,
		waitPing:true
	}).exec(function(err, data) {
	
		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}	
		for(var i = 0; i < data.length; i++) {
			data[i].shopImg = '/images/' + data[i].shopImg
		}
		
		data.forEach(function(item){
			console.log(item)
			item.waitPing = false;
			item.save();
		})
		
		res.json({
			error: 0,
			msg: 'success',
			result: {
				data
			}
		})
	})
})

//取消该订单清空购物车中该订单
router.post('/quxiaodingdan', function(req, res) {
	var id = req.body._id;
	CartModel.findByIdAndRemove({
		"_id": id
	}).exec(function(err) {
		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}
		CartModel.find().exec(function(err, data) {
			if(err) {
				res.json({
					status: '1',
					msg: err.message,
					result: ''
				});
			}
			res.json({
				error: 0,
				msg: '成功',
				result: {
					data
				}
			})

		})

	})

})
//点击发货
router.post('/fukuan', function(req, res) {
	var id = req.body._id;
	CartModel.findById(id).exec(function(err, data) {
		//			console.log(data)
		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}

		data.waitGo = false;
		data.save();
		res.json({
			error: 0,
			msg: 'success',
			result: {
				data
			}
		})
	})
})
// 点击确认收货,让待收货中不显示
router.post('/waitshouhuo', function(req, res) {
	var id = req.body._id;
	CartModel.findById(id).exec(function(err, data) {
		//			console.log(data)
		if(err) {
			res.json({
				status: '1',
				msg: err.message,
				result: ''
			});
		}

		data.waitshou = false;
		data.save();
		res.json({
			error: 0,
			msg: 'success',
			result: {
				data
			}
		})
	})
})

//支付成功之后
router.post('/success', function(req, res) {
	var id = req.cookies.userId;

	CartModel.find(id).exec(function(err, data) {

		if(err) {
			return res.json({
				error: 1,
				msg: "错误"
			});
		}
		data.forEach(function(item) {
			item.daiPay = false;
			item.save();

		})
		res.json({
			error: 0,
			msg: "成功"
		});
	})
})

module.exports = router;